package com.weiyin.examination.application;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.util.SPUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.regex.Pattern;

/**
 * @author njf
 *         Created by lenovo on 2017/9/21.
 */

public class Constant {
    private static Toast mToast = null;
    public static final String NEXT_KNOWLEDGE = "next_knowledge";
    public static final String LAST_KNOWLEDGE = "lastKnowledge";
    public static final int BOTTOM_SIZE = 17;

    public static final String RESP_CODE = "respCode";
    public static final String RESP_INFO = "respInfo";
    public final static String NICKNAME = "nickName";
    public final static String HEAD_IMAGE_URL = "headImageUrl";
    public final static String USER = "user";
    public final static String UNION_ID = "unionId";
    public static final String CENTER = "center";
    public static final String LEFT = "left";
    public static final String RIGHT = "right";
    public static final String YES = "yes";
    public static final String EMPTY = "原位";
    public static final String BOTTOM_RESULT = "bottomResult";
    public static final String BOTTOM_DISMISS = "bottomDismiss";
    public static final String BOTTOM_START = "bottomStart";
    public static final String BOTTOM_END = "bottomEnd";
    public static final String COURSE_A9_DIALOG_RESULT1 = "courseA9DialogResult1";
    public static final String COURSE_A9_DIALOG_RESULT2 = "courseA9DialogResult2";
    public static final String COURSE_A9_DIALOG_RESULT3 = "courseA9DialogResult3";

    public static final int MEDIA_PLAYER_STOP_NUMBER = 3;

    public static final String SUBJECT_URL_CONTAINS = ",";
    public static final String INTERVAL_SIGN = "-";

    public static void showTextToast(Context mContext, String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    //获取版本名
    public static String GetVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    public static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }
    /**
     * 判断字符串是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
    public static void getScreen(Activity activity, String imgUrl, String number) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(), 0, 0, view.getMeasuredWidth(),
                view.getMeasuredHeight());
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();
        if (bitmap != null) {
            try {
                // 获取内置SD卡路径
                String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                // 图片文件路径
                String filePath = sdCardPath + File.separator + imgUrl + ".jpg";
                File file = new File(filePath);
                FileOutputStream os = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                os.flush();
                os.close();
                LogUtil.e("a7888", "存储完成"+ file.getPath());
                SPUtil.setSharedPreferencesData(activity, number, file.getPath());
            } catch (Exception e) {
            }
        }
    }

    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        // 获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }
}
